clear
use data/gss2020panel_r1a.dta

keep samptype-id_2 abany_1a-absingle_1a adults_1a abany_2-absingle_2 advfront_1a affrmact_1a affrmact_2  aid*_1a attend_1a attend_2  babies_1a ballot_1a ballot_2 born_1a cappun_1a cappun_2  childs_1a childs_2 closeblk_1a closewht_1a cohort_1a cohort_2 comprend_1a  coldeg1_1a colhomo_1a colhomo_2 colmil_1a colmil_2 colmslm_1a colmslm_2 colrac_1a colrac_2 conrinc_1a  conrinc_2 consent_1a consent_2 coop_1a cutgovt_1a degree_1a degree_2 dipged_1a dipged_2 discaf*_1a discaf*_2 dwelown_1a dwelown_2 educ_1a educ_2 facebook_1a fair_1a fair_2 fear_1a fear_2 fepol_1a fepol_2 getahead_1a getahead_2 grass_1a grass_2 helpblk_1a helpblk_2 jobsall_1a aidold_1a aidunemp_1a equalize_1a grnlaws_1a hlthcare_1a pricecon_1a lib*_1a lib*_2 makejobs_1a makejobs_2 sparms_1a sparts_1a spenviro_1a sphlth_1a sppolice_1a spschool_1a spunemp_1a poleff*_1a polint_1a polintrst_2 polviews_1a polviews_2 partyid_1a partyid_1b partyid_2 race_1a realinc_1a realrinc_1a region_1a if12who_1a pres12_1a vote12_1a if12who_1b pres12_1b vote12_1b if12who_2 pres12_2 vote12_2 if16who_1b pres16_1b vote16_1b if16who_2 pres16_2 vote16_2 whovote1_2 lkelyvot_2 aprvpres_1a aprvpres_2 spaneng_1a anesid sei10_1a sei10educ_1a sei10inc_1a vote12_1a vote16_2 sex_1a

***
svyset [pweight=wtssall_1a], strata(vstrat_1a) psu(vpsu_1a) singleunit(scaled)
*svyset [pweight=wtssall_2], strata(vstrat_2) psu(vpsu_2) singleunit(scaled)


*** Obama==1 Romney == 2 Thirdparty/other==3
gen prespref12_all= 1 if pres12_1a==1
replace prespref12_all = 1 if pres12_1b==1 & prespref12_all==.
replace prespref12_all = 1 if if12who_1a==1 & prespref12_all==.
replace prespref12_all = 1 if if12who_1b==1 & prespref12_all==.

replace prespref12_all = 2 if pres12_1a==2 & prespref12_all==.
replace prespref12_all = 2 if pres12_1b==2 & prespref12_all==.
replace prespref12_all = 2 if if12who_1a==2 & prespref12_all==.
replace prespref12_all = 2 if if12who_1b==2 & prespref12_all==.

replace prespref12_all = 3 if pres12_1a==3 & prespref12_all==.
replace prespref12_all = 3 if pres12_1b==3 & prespref12_all==.
replace prespref12_all = 3 if if12who_1a==3 & prespref12_all==.
replace prespref12_all = 3 if if12who_1b==3  & prespref12_all==.

****clinton==1 Trump==2

gen prespref16_all= 1 if pres16_1b==1
replace prespref16_all = 1 if pres16_2==1 & prespref16_all==.
replace prespref16_all = 1 if if16who_1b==1 & prespref16_all==.
replace prespref16_all = 1 if if16who_2==1 & prespref16_all==.

replace prespref16_all = 2 if pres16_1b==2 & prespref16_all==.
replace prespref16_all = 2 if pres16_2==2 & prespref16_all==.
replace prespref16_all = 2 if if16who_1b==2 & prespref16_all==.
replace prespref16_all = 2 if if16who_2==2 & prespref16_all==.

replace prespref16_all = 3 if pres16_1b==3 & prespref16_all==.
replace prespref16_all = 3 if pres16_2==3 & prespref16_all==.
replace prespref16_all = 3 if if16who_1b==3 & prespref16_all==.
replace prespref16_all = 3 if if16who_2==3  & prespref16_all==.

gen partyidswitch = 0 if partyid_1a<. & partyid_2<.
replace partyidswitch = 1 if partyid_1a<=2 & partyid_2>=4 & partyid_2<7
replace partyidswitch = 1 if partyid_1a>=4 & partyid_1a<7 & partyid_2<=2
replace partyidswitch = 0 if partyid_1b<=2 & partyid_2<=2
replace partyidswitch = 0 if partyid_1b>=4 & partyid_1b<7 &  partyid_2>=4 & partyid_2<7
replace partyidswitch = 0 if partyid_1b==7
replace partyidswitch = 1 if partyid_1b<=2 & partyid_2>=4 & partyid_2<7
replace partyidswitch = 1 if partyid_1b>=4 & partyid_1b<7 & partyid_2<=2
svyset [pweight=wtssall_2], strata(vstrat_2) psu(vpsu_2) singleunit(scaled)
svy: tab partyidswitch
svyset [pweight=wtssall_1a], strata(vstrat_1a) psu(vpsu_1a) singleunit(scaled)
svy: proportion partyidswitch if samptype==2016

drop partyidswitch

rename yearid YEARID
merge 1:1 YEARID using "anes_timeseries_2020_gss_stata_20220408.dta" , keepusing(V202105x)

gen prespref20 = 1 if V202105x==10
replace prespref20 = 2 if V202105x==11
replace prespref20 = 3 if V202105x==12
replace prespref20 = 1 if V202105x==30
replace prespref20 = 2 if V202105x==31
replace prespref20 = 3 if V202105x==32


gen supportswitch = 0 if prespref16_all!=. & prespref20!=.
replace supportswitch = 1 if prespref16==1 & prespref20==2
replace supportswitch = 1 if prespref16==2 & prespref20==1
svy: proportion supportswitch if samptype==2016

drop supportswitch

** Exclude 3rd parties

gen supportswitch = 0 if prespref16_all<=2 & prespref20<=2
replace supportswitch = 1 if prespref16==1 & prespref20==2
replace supportswitch = 1 if prespref16==2 & prespref20==1
svy: proportion supportswitch if samptype==2016

drop supportswitch

gen vote2020 = 0 if V202105x<=32
replace vote2020 = 1 if V202105x==10
replace vote2020 = 1 if V202105x==11
replace vote2020 = 1 if V202105x==12


gen vote2012 = 0 if vote12_1a<=3
replace vote2012=1 if vote12_1a==1

gen vote2016=0 if vote16_2<=3
replace vote2016=1 if vote16_2==1

recode vote12_1a .d=. .n=.
recode vote16_2 .i=. .n=. .s =. 

tab samptype _merge, col
drop _merge
drop if samptype==2018
drop if panstat==0
drop if panstat==33

** To drop dead people
**drop if panstat==3

**
*keep age_1a attend_1a polint_1a marital_1a bible_1a born_1a pray_1a race_1a cohort_1a coninc_1a coop_1a degree_1a educ

** keep if from 2016 sample
keep if samptype==2016
 
gen female = sex_1a==2
gen black = race_1a==2
gen other = race_1a==3
tab region, gen(regiondum)
 
gen partyid2016 = 1 if partyid_1a<=2
replace partyid2016 = 2 if partyid_1a==4
replace partyid2016 = 2 if partyid_1a==5
replace partyid2016 = 2 if partyid_1a==6
replace partyid2016 = 3 if partyid_1a==3
replace partyid2016 = 4 if partyid_1a==7

gen demstrength2016 = .
replace demstrength2016 = 3 if partyid_1a==0
replace demstrength2016 = 2 if partyid_1a==1
replace demstrength2016 = 1 if partyid_1a==2
gen repstrength2016 = .
replace repstrength2016 = 3 if partyid_1a==6
replace repstrength2016 = 2 if partyid_1a==5
replace repstrength2016 = 1 if partyid_1a==4
 
gen partyid2020 = 1 if partyid_2<=2
replace partyid2020 = 2 if partyid_2==4
replace partyid2020 = 2 if partyid_2==5
replace partyid2020 = 2 if partyid_2==6
replace partyid2020 = 3 if partyid_2==3
replace partyid2020 = 4 if partyid_2==7

gen demstrength2020 = .
replace demstrength2020 = 3 if partyid_2==0
replace demstrength2020 = 2 if partyid_2==1
replace demstrength2020 = 1 if partyid_2==2
gen repstrength2020 = .
replace repstrength2020 = 3 if partyid_2==6
replace repstrength2020 = 2 if partyid_2==5
replace repstrength2020 = 1 if partyid_2==4
 


tab abany_1a, mis
tab abhlth_1a
tab absingle_1a
tab abany_2
tab abhlth_2
tab absingle_2

tab abany_1a abany_2

tab affrmact_1a affrmact_2

tab attend_1a attend_2

tab poleff16_1, mis

recode cohort_1a .n=.  .s=.
recode realinc_1a .i=.  .s=.
recode comprend_1a .d = . .n= .  .s=.
recode coop_1a .d = . .n = .  .s=.
recode degree_1a .n = .  .s=.
recode polviews_1a .d =. .n = . .s=.
recode polviews_2 .d =. .n =. .s=. .i=.
recode abany_1a 2=1 1=0 .d=. .n =. .i=.  .s=.
recode abhlth_1a 2=1 1=0 .d=. .n =.  .i=.  .s=.
recode absingle_1a  2=1 1=0 .d =. .n = .  .i=.  .s=.
recode abany_2 2=1 1=0 .d =. .n = .  .i=.  .s=.
recode abhlth_2 2=1 1=0 .d =. .n = .  .i=. .s=.
recode absingle_2 2=1 1=0 .d =. .n = .  .i=. .s=.
recode affrmact_1a  .d =. .n = .  .i=.  .s=.
recode affrmact_2  .d =. .n = .  .i=.  .s=.
recode attend_1a  .d =. .n = .  .i=.  .s=.
recode attend_2  .d =. .n = .  .i=. .s=.
recode poleff11_1a  .d =. .n = .  .i=.  .s=.
recode poleff16_1a  .d =. .n = .  .i=.  .s=.
recode poleff17_1a  .d =. .n = .  .i=.  .s=.


tab partyid_1a, mis
tab partyid_2, mis

gen pid16 = 0 if partyid_1a==0
replace pid16 = 1 if partyid_1a==1
replace pid16 = 2 if partyid_1a==2
replace pid16 = 3 if partyid_1a==3
replace pid16 = 4 if partyid_1a==4
replace pid16 = 5 if partyid_1a==5
replace pid16 = 6 if partyid_1a==6
replace pid16 = 3 if partyid_1a==7
gen thirdparty16 = 0 if pid16!=.
replace thirdparty16 = 1 if partyid_1a==7

gen pid20 = 0 if partyid_2==0
replace pid20 = 1 if partyid_2==1
replace pid20 = 2 if partyid_2==2
replace pid20 = 3 if partyid_2==3
replace pid20 = 4 if partyid_2==4
replace pid20 = 5 if partyid_2==5
replace pid20 = 6 if partyid_2==6
replace pid20 = 3 if partyid_2==7
gen thirdparty20 = 0 if pid20!=.
replace thirdparty20 = 1 if partyid_2==7

mi set mlong


*gen demvreppref12 = 1 if prespref12_all==1
*replace demvreppref12 = 0 if prespref12_all==2
*gen demvreppref16 = 1 if prespref16_all==1
*replace demvreppref16 = 0 if prespref16_all==2
*gen demvreppref20 = 1 if prespref20==1
*replace demvreppref20 = 0 if prespref20==2

recode coop_1a 4=3

mi register imputed cohort_1a degree_1a realinc_1a coop_1a comprend_1a pid16 pid20 thirdparty16 thirdparty20  prespref12_all prespref16_all prespref20 polviews_1a polviews_2 attend_1a  attend_2  abany_1a abhlth_1a absingle_1a abany_2 abhlth_2 absingle_2 affrmact_1a affrmact_2 poleff11_1a poleff16_1a poleff17_1a vote2012 vote2016 vote2020

mi misstable summarize

mi impute chained (truncreg, ll(1923) ul(1998) omit(i.pid16 i.thirdparty16 i.pid20 i.thirdparty20 i.prespref12_all i.prespref16_all i.prespref20 i.vote2020 i.vote2016 i.vote2012 attend_2 abany_1a abhlth_1a absingle_1a affrmact_1a abany_2 abhlth_2 absingle_2 affrmact_2 poleff11_1a poleff16_1a poleff17_1a)) cohort_1a (ologit,  omit(i.pid16 i.thirdparty16 i.pid20 i.thirdparty20  i.prespref12_all i.prespref16_all i.prespref20 i.vote2020 i.vote2016 i.vote2012 attend_2 abany_1a abhlth_1a absingle_1a affrmact_1a abany_2 abhlth_2 absingle_2 affrmact_2 poleff11_1a poleff16_1a poleff17_1a)) degree_1a  coop_1a comprend_1a (regress, omit(i.pid16 i.thirdparty16 i.pid20 i.thirdparty20  i.prespref12_all i.prespref16_all i.prespref20 i.vote2020 i.vote2016 i.vote2012 attend_2 abany_1a abhlth_1a absingle_1a affrmact_1a abany_2 abhlth_2 absingle_2 affrmact_2 poleff11_1a poleff16_1a poleff17_1a)) realinc_1a (ologit, ascontinuous  omit(i.pid20 i.thirdparty20 i.prespref12_all i.prespref16_all i.prespref20 i.vote2020 i.vote2016 i.vote2012 attend_2 abany_2 abhlth_2 absingle_2 affrmact_2 i.thirdparty16 i.comprend_1a i.coop_1a)) polviews_1a  attend_1a affrmact_1a poleff11_1a poleff16_1a poleff17_1a (logit, ascontinuous  omit(i.pid20 i.thirdparty20 i.thirdparty16 i.prespref12_all i.prespref16_all i.prespref20 i.vote2020 i.vote2016 i.vote2012 attend_2 abany_2 abhlth_2 absingle_2 affrmact_2)) abany_1a abhlth_1a absingle_1a (ologit, omit(i.pid20 i.thirdparty16 i.prespref12_all i.prespref16_all i.prespref20 i.vote2020 i.vote2016 i.vote2012 attend_2 abany_2 abhlth_2 absingle_2 affrmact_2)) pid16 (logit, cond(pid16==3) omit(i.pid20 i.prespref12_all i.prespref16_all i.prespref20 i.vote2020 i.vote2016 i.vote2012 absingle_1a abhlth_1a abany_1a affrmact_1a polviews_2 attend_2 abany_2 abhlth_2 absingle_2 affrmact_2 i.comprend_1a i.coop_1a)) thirdparty16 (mlogit, augment omit(i.pid20 i.thirdparty20 i.prespref20 i.vote2012 i.vote2020 i.vote2016  attend_2 abany_2 abhlth_2 absingle_2 affrmact_2)) prespref12_all (mlogit, augment omit(i.prespref12_all i.pid20 i.thirdparty20 i.vote2012 i.vote2020 i.vote2016  attend_2 abany_2 abhlth_2 absingle_2 affrmact_2)) prespref16_all (logit, omit(i.pid20 i.thirdparty20 i.thirdparty16 i.prespref12_all i.prespref16_all i.prespref20 i.vote2016)) vote2012 (logit, omit(i.pid20 i.thirdparty20 i.thirdparty16 i.prespref12_all i.prespref16_all i.prespref20)) vote2016  (ologit, ascontinuous  omit(i.pid16 i.thirdparty16 i.prespref12_all i.prespref16_all i.vote2016 i.vote2012 i.comprend_1a i.coop_1a poleff11_1a poleff16_1a poleff17_1a i.thirdparty20 i.vote2020)) polviews_2 attend_2 affrmact_2  (logit, ascontinuous  omit(i.pid16 i.thirdparty16 i.prespref12_all i.prespref16_all i.prespref20 i.vote2016 i.vote2012 attend_1a affrmact_1a i.comprend_1a i.coop_1a polviews_1a poleff11_1a poleff16_1a poleff17_1a)) abany_2 abhlth_2 absingle_2 (ologit, omit(i.prespref12_all i.prespref16_all i.vote2020 i.vote2016 i.vote2012 attend_1a abany_1a abhlth_1a absingle_1a affrmact_1a i.thirdparty20)) pid20 (logit, cond(pid20==3) omit(i.pid16 i.prespref12_all i.prespref16_all i.prespref20 i.vote2020 i.vote2016 i.vote2012 attend_1a abany_1a abhlth_1a absingle_1a affrmact_1a  i.comprend_1a i.coop_1a abhlth_2 absingle_2 abany_2 affrmact_2)) thirdparty20 (mlogit, augment omit(i.pid16 i.thirdparty16 i.prespref12_all i.vote2012 i.vote2020 i.vote2016 polviews_1a absingle_1a abhlth_1a abany_1a affrmact_1a poleff11_1a poleff16_1a poleff17_1a i.comprend_1a i.coop_1a abhlth_2 absingle_2 abany_2 affrmact_2 i.degree_1a regiondum2-regiondum9)) prespref20 (logit, omit(i.prespref12_all i.prespref16_all i.prespref20 attend_1a abany_1a abhlth_1a absingle_1a affrmact_1a i.pid16 i.thirdparty16)) vote2020 = regiondum2-regiondum9 black female, add(40) burnin(20) rseed(49301) chaindots

*** 
*use impstats.dta, clear

mi estimate: svy: proportion pid16 
mi estimate: svy: proportion pid20

mi passive: gen partyidswitch = 0
mi passive: replace partyidswitch = 1 if pid16<=2 & pid20>=4 & pid20<7
mi passive: replace partyidswitch = 1 if pid16>=4 & pid20<7 & pid20<=2

mi estimate: svy: proportion partyidswitch 

mi estimate: svy: proportion partyidswitch, over(pid16)
mi estimate: svy: proportion partyidswitch, over(degree_1a)


mi estimate, saving(mi_mlogit16, replace): mlogit prespref16 i.pid16 polviews_1a thirdparty16 cohort_1a female black attend_1a abany_1a, base(1)
mi predict xb16_1_mi using mi_mlogit16
mi predict xb16_2_mi using mi_mlogit16, equation(#2)
mi predict xb16_3_mi using mi_mlogit16, equation(#3)
mi xeq 0: summarize xb16_1_mi xb16_2_mi xb16_3_mi

mi passive: gen demvrepphat16 = 1/(1+exp(xb16_2_mi))
mi estimate: mean demvrepphat16
mi passive: gen simdem16 = uniform()<=demvrepphat16
mi passive: gen demvrepchoice16 = 0 if prespref16<=2 
mi passive: replace demvrepchoice16 = 1 if prespref16==1
mi passive: replace demvrepchoice16 = simdem16 if demvrepchoice16==.

mi estimate: svy: proportion prespref16 demvrepchoice16

mi estimate, saving(mi_mlogit20, replace): mlogit prespref20 i.pid20 polviews_2 thirdparty20 cohort_1a female black attend_2 abany_2, base(1) 

mi predict xb20_1_mi using mi_mlogit20
mi predict xb20_2_mi using mi_mlogit20, equation(#2)
mi predict xb20_3_mi using mi_mlogit20, equation(#3)
mi xeq 0: summarize xb20_1_mi xb20_2_mi xb20_3_mi

mi passive: gen demvrepphat20 = 1/(1+exp(xb20_2_mi))
mi estimate: mean demvrepphat20
mi passive: gen simdem20 = uniform()<=demvrepphat20
mi passive: gen demvrepchoice20 = 0 if prespref20<=2
mi passive: replace demvrepchoice20 = 1 if prespref20==1
mi passive: replace demvrepchoice20 = simdem20 if demvrepchoice20==.


mi estimate: svy: proportion prespref20 demvrepchoice20

mi passive: gen supportswitch = 0 if prespref16!=. & prespref20!=.
mi passive: replace supportswitch = 1 if prespref16==1 & prespref20==2
mi passive: replace supportswitch = 1 if prespref16==2 & prespref20==1

mi estimate: svy: proportion supportswitch

drop supportswitch


***************
** no 3rd party
***************

** make sure imputed values are consistent

mi passive: gen supportswitch = 0 
mi passive: replace supportswitch = 1 if demvrepchoice16!=demvrepchoice20
mi estimate: svy: proportion supportswitch 



***save "panelimpute_gss2016_2020.dta", replace 
